package com.yunchao.bank.business.pesentation.screen.info

import com.yunchao.feature.base.domain.result.errorMsg
import com.yunchao.feature.base.presentation.nav.NavManager
import com.yunchao.feature.base.presentation.viewmodel.BaseEffect
import com.yunchao.feature.base.presentation.viewmodel.BaseState
import com.yunchao.feature.base.presentation.viewmodel.BaseViewModel
import com.yunchao.bank.business.domain.model.BusinessInfoModel
import com.yunchao.bank.business.pesentation.screen.info.BusinessInfoViewModel.UiEffect
import com.yunchao.bank.business.pesentation.screen.info.BusinessInfoViewModel.UiState

internal class BusinessInfoViewModel(private val navManager: NavManager) :
    BaseViewModel<UiState, Nothing, UiEffect>(UiState.Idle) {


    fun navigateToDetails(model: BusinessInfoModel) {
        navManager.navigate(
            BusinessInfoFragmentDirections.actionBusinessInfoFragmentToBusinessDetailsFragment(
                taskId = model.taskId,
            )
        )
    }

    private fun launchWithLoading(block: suspend () -> Unit) = safeLaunch(
        onStart = { postSideEffect(UiEffect.ShowLoading) },
        onFinally = { postSideEffect(UiEffect.HideLoading) },
        onError = { postSideEffect(UiEffect.ShowToast(it.errorMsg)) },
        block = block
    )



    internal sealed interface UiState : BaseState {
        data object Idle : UiState
    }

    internal sealed interface UiEffect : BaseEffect {
        data object ShowLoading : UiEffect
        data object HideLoading : UiEffect
        data class ShowToast(val msg: String) : UiEffect
    }
}